package org.example.controller;

import com.github.pagehelper.Page;
import org.example.pojo.Order;
import org.example.pojo.PageBean;
import org.example.pojo.Result;
import org.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获取订单详情
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/{id}")
    public Result<Order> getById(@PathVariable Integer id) {
        Order order = orderService.getById(id);
        return Result.success("获取成功", order);
    }

    /**
     * 添加订单
     */
    @PostMapping
    public Result<Object> add(@RequestBody Order order) {
        System.out.println("order:" + order);
        Integer orderId = orderService.add(order);
        return Result.success("创建成功", orderId);
    }

    /**
     * 订单列表
     * @param orderNumber 订单编号
     * @return 订单列表
     */
    @GetMapping("/list")
    public Result<PageBean<Order>> list(int pageNum, int pageSize, String orderNumber) {
        PageBean<Order> page = orderService.list(pageNum, pageSize,orderNumber);
        return Result.success(page);
    }

    /**
     * 修改订单
     * @param order 订单信息
     * @return 操作结果
     */
    @PutMapping
    public Result<Void> update(@RequestBody Order order) {
        orderService.update(order);
        return Result.success();
    }

    /**
     * 删除订单
     * @param id 订单ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        orderService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除订单
     * @param ids 订单ID数组
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDelete(@RequestBody Integer[] ids) {
        orderService.batchDelete(ids);
        return Result.success();
    }
}
